/**
 * <b>项目名：</b>meizuapi<br/>
 * <b>包名：</b>com.meizu.api.meizuapi<br/>
 * <b>文件名：</b>ApiServlet.java<br/>
 * <b>版本信息：</b> @version 1.0.0<br/>
 * <b>日期：</b>2013-4-17-下午4:45:14<br/>
 * <b>Copyright (c)</b> 2013魅族公司-版权所有<br/>
 *
 */

package org.apitool.main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.jetty.Request;

/**
 * <b>类名称：</b>ApiServlet<br/>
 * <b>类描述：</b>
 * 
 * <pre>
 </pre>
 * 
 * <br/>
 * <b>创建人：</b>张军强<br/>
 * <b>修改人：</b>张军强<br/>
 * <b>修改时间：</b>2013-4-17 下午4:45:14<br/>
 * <b>修改备注：</b><br/>
 * 
 * @version 1.0.0<br/>
 */

public class ApiServlet extends HttpServlet {

	private TestBase base = new TestBase();

	private List<PostParameter> buildParams(HttpServletRequest req, HttpServletResponse resp) {
		List<PostParameter> listPost = new ArrayList<PostParameter>();

		Enumeration names = req.getParameterNames();
		while (names != null && names.hasMoreElements()) {
			String name = names.nextElement().toString();
			// if (name.matches("p\\d+")) {
			listPost.add(new PostParameter(name, req.getParameter(name)));
			// }
		}
		return listPost;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// HttpSession session = req.getSession(true);
		Integer session = 60;
		if (session == null) {

		} else {
			List<PostParameter> postParams = new ArrayList<PostParameter>();
			String host = req.getParameter("__host");
			String str = getBase().post(host + req.getRequestURI(), buildParams(req, resp));
			resp.getWriter().write(str);
			resp.getWriter().flush();
			resp.setStatus(HttpServletResponse.SC_OK);
			((Request) req).setHandled(true);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	public TestBase getBase() {
		return base;
	}

	public void setBase(TestBase base) {
		this.base = base;
	}

}
